DATA SEGMENT
    str     DB 20 DUP(2), 18 DUP(1), 0,3
    substr  DB 19 DUP(1), 0 
    found   DB 0
    pos     DW 0
    len     DW 0    ; the length of substr
DATA ENDS

CODE SEGMENT
    ASSUME CS:CODE, DS:DATA
START:
    MOV AX, DATA
    MOV DS, AX
    
    MOV found, 0
    MOV BX, 0FFFFH  ; the main pointer
    ; the outer loop
LO: INC BX
    CMP str[BX], 0       
    JZ  OT          ; goto out if traversal is done
    MOV AL, substr
    CMP str[BX], AL
    JZ  LI          ; goto the inner loop
    JMP LO  
    
OT: MOV AH, 4CH     ; DONE
    INT 21H
    
LI: MOV pos, BX
    MOV SI, 1       ; the offset pointer
    ; to compare str[BX+SI] and substr[SI]
CP: CMP substr[SI], 0
    JZ  EQ          ; goto equal if traversal is done
    CMP substr[SI+1], 0
    JNZ CB          ; compare a single byte
    DEC SI 
CB: MOV AX, WORD PTR substr[SI]
    CMP WORD PTR str[BX+SI], AX
    JNZ LO
    INC SI
    INC SI
    JMP CP
    
EQ: MOV AL, 0FFH
    MOV found, AL
    MOV len, SI
    JMP OT

CODE ENDS
END START
